package com.china.zhuoyue.ui.fragment

import android.graphics.Paint
import android.os.Bundle
import android.view.View
import com.china.zhuoyue.R
import com.china.zhuoyue.api.business.area.BusICircle
import com.china.zhuoyue.api.business.area.launchUIBusinessArea
import com.china.zhuoyue.databinding.FragemntBusinessAreaBinding
import com.china.zhuoyue.databinding.ItemBusinessAreaBinding
import com.china.zhuoyue.map.getLocation
import com.china.zhuoyue.ui.activity.business.area.BusinessAreaDetailActivity
import com.china.zhuoyue.ui.base.BaseFragment
import com.china.zhuoyue.ui.base.BaseRecyclerAdapter
import com.china.zhuoyue.ui.base.BaseViewHolder
import com.china.zhuoyue.ui.ext.format
import com.china.zhuoyue.ui.ext.getString
import com.china.zhuoyue.ui.ext.showLoadingBeforeSuccessCallback
import com.china.zhuoyue.ui.ext.startActivityExt
import com.china.zhuoyue.ui.helper.RefreshHelper
import com.china.zhuoyue.ui.utils.GlideUtils

class BusinessAreaFragment : BaseFragment<FragemntBusinessAreaBinding>() {
    private val mBusinessAreaAdapter = BusinessAreaAdapter()
    private lateinit var mRefreshHelper: RefreshHelper<BusICircle>
    private var latitude: Double? = null
    private var longitude: Double? = null
    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.recyclerView, mBusinessAreaAdapter) { onReload() }
        mRefreshHelper.mLoadService?.showLoadingBeforeSuccessCallback()
        getLocation {
            if (it != null) {
                latitude = it.latitude
                longitude = it.longitude
            }
            onReload()
        }
    }

    private fun onReload() {
        launchUIBusinessArea({
            busicircleIndex(
                RefreshHelper.DEFAULT_LIMIT,
                mRefreshHelper.page,
                latitude,
                longitude
            )
        }, mRefreshHelper.handApiObserver())
    }
}

class BusinessAreaAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemBusinessAreaBinding>, BusICircle>() {

    override fun onBindViewHolder(holder: BaseViewHolder<ItemBusinessAreaBinding>, bean: BusICircle, position: Int) {
        holder.vb.apply {
            bean.apply {
                GlideUtils.intoView(ivImage.context, coverImg, ivImage)
                tvTitle.text = title
                tvSecondTitle.text = secondTitle
                tvSold.text = getString(R.string.text_business_area_sold, sold)
                tvPrice.text = getString(R.string.text_rmb, price.format())
                tvMarketPrice.text = getString(R.string.text_rmb, marketPrice.format())
                tvMarketPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                tvDistance.text = "${distance}m"
                setViewClick(position, root)
            }
        }
    }

    override fun subOnClickAdapter(v: View, position: Int) {
        v.context.startActivityExt<BusinessAreaDetailActivity> {
//            putExtra(BusinessAreaDetailActivity.ACTIVITY_ID, mBusICircle.id)
//            putExtra(BusinessAreaDetailActivity.DISTANCE, mBusICircle.distance)
            putExtra(BusinessAreaDetailActivity.BUS_ICIRCLE, getItem(position))
        }
    }
}